﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("SelectedValue")]
	public partial class DicSelector : Hxsoft.AppV1.UserControl
	{
		public bool Enabled
		{
			get { return CtrSelector.Enabled; }
			set { CtrSelector.Enabled = value; }
		}

		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => (int?)ViewState["SiteId"];
				return loadedSiteId.Value;
			}
			set
			{
				if (loadedSiteId.Setter == null)
					loadedSiteId.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["SiteId"] = v;
					};
				loadedSiteId.Value = value;
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["ClassCode"] = v;
					};
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<int> loadedClassId;
		protected int ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => { return DicClasses.GetIdentifier(SiteId, ClassCode).Value; };
				return loadedClassId.Value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicValueType>(ViewState["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
			set
			{
				if (loadedValueType.Setter == null)
					loadedValueType.Setter = v => ViewState["ValueType"] = v;
				loadedValueType.Value = value;
			}
		}

		private LoadedValue<DicTextType> loadedTextType;
		public DicTextType TextType
		{
			get
			{
				if (loadedTextType.Loader == null)
					loadedTextType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicTextType>(ViewState["TextType"], DicTextType.Name);
				return loadedTextType.Value;
			}
			set
			{
				if (loadedTextType.Setter == null)
					loadedTextType.Setter = v => ViewState["TextType"] = v;
				loadedTextType.Value = value;
			}
		}

		public string SelectedValue
		{
			get { return CtrSelector.SelectedValue; }
			set
			{
				CtrSelector.DataBind();
				if(CtrSelector.Items.FindByValue(value) != null)
					CtrSelector.SelectedValue = value;
			}
		}


		private bool isBindCtrSelector = false;
		protected void CtrSelector_DataBinding(object sender, EventArgs e)
		{
			if (isBindCtrSelector) return;
			CtrSelector.DataSource = Dictionaries.GetItems(ClassId, null);
			CtrSelector.DataTextField = TextType == DicTextType.Value ? "Value" : "Name";
			CtrSelector.DataValueField = ValueType == DicValueType.Value ? "Value" : "DicId";
		}

		protected void CtrSelector_DataBound(object sender, EventArgs e)
		{
			CtrSelector.EnsurePrompt();

			CtrSelector.DataSource = null;
			isBindCtrSelector = true;
		}

		public void Reset()
		{
			isBindCtrSelector = false;
			CtrSelector.DataBind();
		}
	}
}